package com.pan.behavioral.chain;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

abstract class Node<T>{

    private Node nextNode = null;

    public Node setNext(Node next){
        this.nextNode=next;
        return nextNode;
    }

    public void response(T g){
        handle(g);
        if(nextNode!=null){
            nextNode.response(g);
        }
    }

    public abstract void handle(T obj);

}

class LeftCircle extends Node<Graphics>{public void handle(Graphics g){g.fillOval(50,50,50,50);}}

class RightCircle extends Node<Graphics>{public void handle(Graphics g){g.fillOval(100,50,50,50);}}

class RedColor extends Node<Graphics>{public void handle(Graphics g){g.setColor(Color.RED);}}

class OneOneFour extends Node<Graphics>{public void handle(Graphics g){g.drawString("1145141919810",10,40);}}

/**
 * 太低俗了
 */
public class Animal{

    public static void main(String args[]){

        Image image = new BufferedImage(200,150,1);

        Graphics g = image.getGraphics();

        Node chain = new RedColor();

        chain.setNext(new LeftCircle()).setNext(new RightCircle()).setNext(new OneOneFour());

        chain.response(g); g.dispose();

        JFrame frame = new JFrame("野兽先辈");

        frame.setDefaultCloseOperation(3);

        frame.add(new JLabel(new ImageIcon(image)));

        frame.pack(); frame.setVisible(true);


    }

}

